package com.beamfield.controller.manage.material;

import com.alibaba.fastjson.JSON;
import com.beamfield.common.ApiResult;
import com.beamfield.common.PageResult;
import com.beamfield.controller.common.BaseController;
import com.beamfield.enums.HttpResultEnum;
import com.beamfield.model.Demand;
import com.beamfield.model.DemandDetail;
import com.beamfield.modelVo.DemandDetailVo;
import com.beamfield.modelVo.SuppliesDemandVo;
import com.beamfield.service.manage.material.DemandService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 需求计划
 */
@Controller
@RequestMapping("/cms/material/demand")
public class DemandController extends BaseController {
    
     private final DemandService demandService;

    @Autowired
    public DemandController(DemandService demandService) {
        this.demandService = demandService;
    }


    @RequestMapping(value = "/demand_list", method = RequestMethod.GET)
    public ModelAndView demandList() {
        return new ModelAndView();
    }

    @RequestMapping(value = "/addDemand", method = RequestMethod.GET)
    public ModelAndView addDemand() {
        return new ModelAndView();
    }

    @RequestMapping(value = "/demandDetails", method = RequestMethod.GET)
    public ModelAndView demandDetails() {
        return new ModelAndView();
    }

    /**
     * 审批通过需求计划
     * @param request
     * @return
     */
    @RequestMapping(value = "/doApproved",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult doApproved(HttpServletRequest request){
        String id = request.getParameter("id");
        String appr = request.getParameter("appr");
        if(StringUtils.isEmpty(id) || StringUtils.isEmpty(appr)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        Long userId = super.getUserId(request);
        try{
            return demandService.updateApprove(appr, id, userId);
        }catch (Exception e){
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
    }

    /**
     * 获取需求计划
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/readDemandData", method = RequestMethod.GET)
    @ResponseBody
    public PageResult demandList(HttpServletRequest request) {
        Long beamId = super.getBeamId(request);
        String projectId = request.getParameter("projectId");
        String demandName = request.getParameter("demandName");
        String appr = request.getParameter("appr");
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        List<Demand> demandPlanList = demandService.getdemand(projectId, demandName, offset, limit, appr, beamId);
        long total = demandService.countdemand(projectId, demandName, appr, beamId);
        return new PageResult(total, Long.parseLong(offset), demandPlanList);
    }
    /**
     * 获取需求计划明细列表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/readDemandDetailsData", method = RequestMethod.GET)
    @ResponseBody
    public PageResult demandDetailsList(HttpServletRequest request) {
        String demandId = request.getParameter("demandId");
        String projectId = request.getParameter("projectId");
        if(StringUtils.isEmpty(demandId)||StringUtils.isEmpty(projectId)){
            return null;
        }
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        String suppliesName = request.getParameter("suppliesName");
        List<DemandDetailVo> demandDetailVoList = demandService.getdemandDetails(demandId, suppliesName, offset, limit, projectId);
        long total = demandService.countdemandDetails(demandId, suppliesName);
        if(offset==null){
            offset = "-1";
        }
        return new PageResult(total, Long.parseLong(offset), demandDetailVoList);
    }
    /**
     * 新增需求计划
     *
     * @param demandDetailJson 需求明细列表
     * @param demandName       需求计划名称
     * @return
     */
    @RequestMapping(value = "/doSave", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult doSave(String demandDetailJson, String demandName, String projectId, HttpServletRequest request) {
        ApiResult apiResult = new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        long beamId = super.getBeamId(request);
        long userId = super.getUserId(request);
        if (StringUtils.isEmpty(demandDetailJson)) {
            apiResult.setResult(HttpResultEnum.MIS_PARAM.getCode());
            apiResult.setMessage(HttpResultEnum.MIS_PARAM.getMessage());
            return apiResult;
        }
        if (StringUtils.isEmpty(projectId)) {
            apiResult.setResult(HttpResultEnum.MIS_PARAM.getCode());
            apiResult.setMessage(HttpResultEnum.MIS_PARAM.getMessage());
            return apiResult;
        }
        if (StringUtils.isEmpty(demandName)) {
            apiResult.setResult(HttpResultEnum.MIS_PARAM.getCode());
            apiResult.setMessage("请输入需求计划名称");
            return apiResult;
        }
        try {
            List<DemandDetail> demandDetailList = JSON.parseArray(demandDetailJson, DemandDetail.class);
            if (demandDetailList == null || demandDetailList.isEmpty()) {
                return apiResult;
            }
            apiResult = demandService.save(demandDetailList, demandName, Long.parseLong(projectId), beamId, userId);
        } catch (Exception e) {
            e.printStackTrace();
            apiResult.setCode(HttpResultEnum.EXCEPTION.getCode());
            apiResult.setMessage(HttpResultEnum.EXCEPTION.getMessage());
        }
        return apiResult;
    }

    /**
     * 获取材料设计量和已需求量
     * @param request
     * @return
     */
    @RequestMapping(value = "/getSuppliesDemand", method = RequestMethod.GET)
    @ResponseBody
    public ApiResult getSuppliesDemand(HttpServletRequest request){
        ApiResult apiResult = new ApiResult();
        String projectId = request.getParameter("projectId");
        String suppliesId = request.getParameter("suppliesId");
        if(StringUtils.isEmpty(projectId)||StringUtils.isEmpty(suppliesId)){
            apiResult.setCode(HttpResultEnum.MIS_PARAM.getCode());
            apiResult.setMessage(HttpResultEnum.MIS_PARAM.getMessage());
            return apiResult;
        }
        SuppliesDemandVo suppliesDemandVo = demandService.getSuppliesPlan(projectId, suppliesId);
        apiResult.setResult(suppliesDemandVo);
        return apiResult;
    }
}
